# -*- coding:utf-8 -*-
'''
Created on 2012-2-14

@author: Administrator
'''
import base64, zlib, json, md5
from Crypto.Cipher import AES
class cipher_helper():
    _base64_altchars = ['*','|']
    def __init__(self):
        pass
    def do_md5(self, in_str):
        return md5.new(in_str).digest()
    def do_base64_encode(self, in_str):
        return base64.b64encode(in_str, self._base64_altchars)
    def do_base64_decode(self, in_str):
        return base64.b64decode(in_str, self._base64_altchars)
    def do_zlib_compress(self, in_str):
        return zlib.compress(in_str)
    def do_zlib_decompress(self, in_str):
        return zlib.decompress(in_str)
    def do_compress_b64enc(self, in_str):
        return self.do_base64_encode(self.do_zlib_compress(in_str))
    def do_b64dec_uncompress(self, in_str):
        return self.do_zlib_decompress(self.do_base64_decode(in_str))
    def do_json_dump(self, in_obj):
        return json.dumps(in_obj)
    
    def encode(self, in_obj):
        in_str = json.dumps(in_obj)
        return self.do_compress_b64enc(in_str)
    def decode(self, in_str):
        in_dec_str = self.do_base64_decode(in_str)
        in_obj = json.loads(in_dec_str)
        return in_obj

cipherhelper = cipher_helper()

if __name__ == '__main__':
    helper = cipher_helper()
    str1 = 'help me, please'
    str_zlib = helper.do_zlib_compress(str1)
    str_b64_zlib = helper.do_base64_encode(str_zlib)
    print str_zlib
    print str_b64_zlib
    str_b64d = helper.do_base64_decode(str_b64_zlib)
    str_zlibd_b64d = helper.do_zlib_decompress(str_b64d)
    print str_b64d
    print str_zlibd_b64d
